/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.taxigang.server;

import java.io.Serializable;
import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;

import com.google.appengine.api.datastore.Key;
import com.taxigang.shared.ChitInfo;
import com.taxigang.shared.TaxiGanguserInfo;

/**
 *
 * @author hall
 */
@Entity
@Table(name = "CHIT")
@NamedQueries({	@NamedQuery(	name = "Chit.findAll", 				query = "SELECT a FROM Chit a "), 
				@NamedQuery(	name = "Chit.findByTaxiGang", 	    query = "SELECT a FROM Chit a WHERE a.taxigangId = :taxigangId order by a.date DESC")}) 
public class Chit implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "CHITKEY", nullable = false)
    private Key key;
    @Column(name = "TAXIGANGID", nullable = true)
    private Long taxigangId;
    @Column(name = CHITTEXT, nullable = true)
    private String chitText;    
    @Column(name = DATE, nullable = false)
    private Date date;
    @Column(name = AUTHORNAME, nullable = true)
    private String authorName;
    @Column(name = MOBILENO, nullable = true)
    private String mobileNo;

    public static final  String DATE="DATE";
    public static final  String CHITTEXT="CHITTEXT";
    public static final  String AUTHOR="AUTHOR";
    public static final  String AUTHORNAME="AUTHORNAME";
    public static final  String MOBILENO="MOBILENO";
    
    
    @ManyToOne(fetch = FetchType.LAZY)
    private TaxiGang taxiGang;
    
    public Chit(long taxigangId, TaxiGanguserInfo author, String chitText, String mobileNo) {
    	this.taxigangId = taxigangId;
    	this.date = new Date();
    	this.chitText = chitText;
    	this.authorName= author.getDisplayName();
    	this.mobileNo= mobileNo;

    }
    
    public Chit(String author, String chitText, String mobileNo) {
    	this.date = new Date();
    	this.chitText = chitText;
    	this.authorName= author;
    	this.mobileNo= mobileNo;

    }
//    
//    public Chit(TaxiGanguserInfo author, String chitText, String mobileNo) {
//    	this.date = new Date();
//    	this.chitText = chitText;
//    	this.authorName= author.getDisplayName();
//    	this.mobileNo= mobileNo;
//
//    }
    
	public long getId() {
		return key.getId();
	}

    public ChitInfo getInfo(){
    	return new ChitInfo(chitText,date,authorName, mobileNo);
    }

    public String getAuthorName() {
        return authorName;
    }
    
    public void setAuthorName(String name) {
        authorName = name;
    }
    
    /**
     * Get date
     * @return
     */
    public Date getDate() {
        return date;
    }
    
    public void setDate(Date d) {
        date = d;
    }
    
    /**
     * Get chit text
     * @return
     */
    public String getChitText() {
        return chitText;
    }

    public void  setChitText(String text) {
        chitText=text;
    }


    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Chit)) {
            return false;
        }
        Chit other = (Chit) object;
        if ((this.key == null && other.key != null) || (this.key != null && !this.key.equals(other.key))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "taxigangsystem.beans.TaxiGang[taxigangid=" + key.getId() + "]";
    }

}
